package binaryTree;

/**
 * 设计循环队列
 * */
class MyCircularQueue {
    private int[] elem;
    private int usedSize;
    private int front;
    private int rear;
    private static final int DEFAULT_CAPACITY = 10;

    public MyCircularQueue(int k) {
        if(k<=0) {
            this.elem = new int[DEFAULT_CAPACITY];
        } else{
            this.elem = new int[k];
        }
        this.front = 0;
        this.rear = 0;
        this.usedSize = 0;
    }

    //入队列
    public boolean enQueue(int value) {
        //判满，若是满，return false
        if(isFull()) {
            return false;
        }
        int index = (this.rear+1)%elem.length;

        this.elem[this.rear] = value;
        //判空，若空，对头节点赋值
        if(isEmpty()) {
            this.front = this.rear;
        }
        this.rear = index;
        this.usedSize++;
        return true;
    }


    //出队列
    public boolean deQueue() {
        //判空，若是空，return false
        if(isEmpty()) {
            return false;
        }
        this.front = (this.front+1)%elem.length;
        this.usedSize--;
        return true;
    }
    
    public int Front() {
        if(isEmpty()) {
            return -1;
        } else {
            return this.elem[this.front];
        }
    }

    /**
     * 目前的问题是：
     * 当front不再指向0位置时 返回尾需要如何返*/
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        if(isFull()&&this.rear==0) {
            return this.elem[this.usedSize-1];
        } else {
            return this.elem[this.rear-1];
        }
    }
    
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
    
    public boolean isFull() {
        return this.elem.length == usedSize;
    }
}